package jwdt.xadmin.work.service.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.system.bean.TUser;
import jwdt.xadmin.work.bean.TWorkReport;
import jwdt.xadmin.work.service.WorkReportManageService;

import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;
import com.ezerg.web.ServletActionContext;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class WorkReportManageServiceImpl implements WorkReportManageService{

	private BaseDao<TWorkReport> baseDao;
	
	public void setBaseDao(BaseDao<TWorkReport> baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public PageUtil doList(PageUtil pu,TWorkReport obj,TUser user) throws Exception{
		String where = "1=1";
		String sql = " order by reportid desc";
		if(user != null) {// 限制用户
			where += " and userid = " + user.getUserid();
		}
		Integer depid = obj.getDepid();
		String starttime = obj.getStarttime();
		String stoptime = obj.getStoptime();
		if(depid != null) {
			where += " and depid=" + depid;
		}
		if(!StringUtil.nullOrBlank(starttime)) {
			where += " and starttime>'" + starttime + "'";
		}
		if(!StringUtil.nullOrBlank(stoptime)) {
			where += " and stoptime<'" + stoptime + "'";
		}
		pu.doList(TWorkReport.class,where + sql);
		//创造json数据对象
		return pu;
	}
	
	@Override
	public String doAddact(TWorkReport obj,TUser user) throws Exception {
		obj.setUserid(user.getUserid());
		obj.setDepid(user.getDepid());
		obj.setZt(0);
		String ret = baseDao.doAddObj(obj);
		saveWorkReportWord(obj,user);
		return ret;
	}

	@Override
	public String doEditact(TWorkReport obj, int id,TUser user) throws Exception {
		saveWorkReportWord(obj,user);
		return baseDao.doUpdateObj(TWorkReport.class, id, obj);
	}
	
	@Override
	public String doDelact(int id) throws Exception {
		return baseDao.doDelObj(TWorkReport.class, id);
	}

	@Override
	public TWorkReport doLoad(int id) throws Exception {
		return baseDao.doLoadObj(TWorkReport.class, id);
	}
	
	@Override
	public String doUpdateState(int id, String result) throws Exception {
		return baseDao.doIntUpdateState(TWorkReport.class, "reportid="+id, result);
	}
	
	@SuppressWarnings({"unchecked","rawtypes"})
	public void saveWorkReportWord(TWorkReport obj,TUser user){
		ServletContext sc = ServletActionContext.getServletContext();
		String basePath = sc.getRealPath("/");
		String uploadPath = basePath.replace("\\", "/") + "WEB-INF/files/workreport/";
		String filePath = uploadPath + "/" + user.getUsername()+"/";
		String fileName = obj.getReportid()+".doc";
		
		Map dataMap = new HashMap();
		dataMap.put("user", user.getFullname());
		dataMap.put("dt",DateUtil.getDate());
		dataMap.put("starttime",obj.getStarttime());
		dataMap.put("stoptime",obj.getStoptime());
		dataMap.put("content", obj.getContent().split(";"));
		dataMap.put("nextcontent", obj.getNextcontent().split(";"));
		dataMap.put("addcontent", obj.getAddcontent().split(";"));
		Configuration configuration= new Configuration();
		try {
			//configuration.setDirectoryForTemplateLoading(new File(basePath.replace("\\", "/")+"WEB-INF/template/"));
			configuration.setClassForTemplateLoading(WorkReportManageServiceImpl.class, "");
			Template template = configuration.getTemplate("workreport.xml", "UTF-8");
			  //输出文件
            File outFile = new File(filePath+fileName);
            //如果输出目标文件夹不存在，则创建
            if (!outFile.getParentFile().exists()){
                outFile.getParentFile().mkdirs();
            }
            //将模板和数据模型合并生成文件 
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
            //生成文件
            template.process(dataMap, out);
            //关闭流
            out.flush();
            out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
